package com.bac.rentmap.distance;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Required;

public class RectangleDistanceCheckPointProvider implements
        DistanceCheckPointProvider {
    private double minLat;
    private double maxLat;

    private double minLon;
    private double maxLon;

    private int pointCount;

    public List<DistanceCheckPoint> retrieveDistanceCheckPoints(String city) {
        double dLat = maxLat - minLat;
        double dLon = maxLon - minLon;
        double area = dLat * dLon;
        double step = Math.sqrt(area / pointCount);
        List<DistanceCheckPoint> res = new ArrayList<DistanceCheckPoint>();
        for (int i = 0; i < dLon / step + 1; i++) {
            for (int j = 0; j < dLat / step + 1; j++) {
                double lat = minLat + i * step;
                double lon = minLon + j * step;
                res.add(new DistanceCheckPoint(lat, lon));
                if (res.size() == pointCount)
                    return res;
            }
        }
        throw new RuntimeException("Only " + res.size()
                + " check points calculated while need " + pointCount);
    }

    public void setMinLat(double minLat) {
        this.minLat = minLat;
    }

    public void setMaxLat(double maxLat) {
        this.maxLat = maxLat;
    }

    public void setMinLon(double minLon) {
        this.minLon = minLon;
    }

    public void setMaxLon(double maxLon) {
        this.maxLon = maxLon;
    }

    @Required
    public void setPointCount(int pointCount) {
        this.pointCount = pointCount;
    }
}
